# Advice: This file is running after system installs done.
## Note: This must use absolute path, don't use ~ instead of /home/hp
current_dir="/home/hp//dotfiles"
cd $current_dir

# Using pacman to install package
pacman_packages="pacman-packages.txt"

array=()

while IFS= read -r line; do
    array+=("$line")
done < "$pacman_packages"

sudo pacman -S --needed "${array[@]}"

# Config keyd
cd /etc/keyd/
file_type=$(ls -lah | grep default.conf | awk '{print substr($res, 0, 1)}')
expect_type="l"
if [ "$expect_type" != "$file_type" ]; then
  sudo ln -s "${current_dir}/keyd/default.conf" /etc/keyd/default.conf
  source ~/.bashrc
fi

# Config shell
cd ~
file_type=$(ls -lah | grep .bashrc | awk '{print substr($res, 0, 1)}')
expect_type="l"
if [ "$expect_type" != "$file_type" ]; then
  rm ~/.bashrc
  ln -s "${current_dir}/shell/.bashrc" ~/.bashrc
  source ~/.bashrc
fi

# Config xorg
cd ~
file_type=$(ls -lah | grep .xinitrc | awk '{print substr($res, 0, 1)}')
expect_type="l"
if [ "$expect_type" != "$file_type" ]; then
  ln -s "${current_dir}/xorg/.xinitrc" ~/.xinitrc
fi

# Config alacritty
cd ~/.config
file_type=$(ls -lah | grep alacritty | awk '{print substr($res, 0, 1)}')
expect_type="l"
if [ "$expect_type" != "$file_type" ]; then
  ln -s "${current_dir}/alacritty/" ~/.config/
fi

# Config nodejs
npm config set registry https://registry.npmmirror.com
pnpm config set registry https://registry.npmmirror.com

# Config nvim
cd ~/.config/
file_type=$(ls -lah | grep nvim | awk '{print substr($res, 0, 1)}')
expect_type="l"
if [ "$expect_type" != "$file_type" ]; then
  sudo npm install -g neovim
  ln -s "${current_dir}/nvim/" ~/.config/
fi

# Config cargo
cd ~/.cargo/
file_type=$(ls -lah | grep conf | awk '{print substr($res, 0, 1)}')
expect_type="l"
if [ "$expect_type" != "$file_type" ]; then
  ln -s "${current_dir}/cargo/config" ~/.cargo/config
fi

# Config cargo
cd ~/.config/
file_type=$(ls -lah | grep rofi | awk '{print substr($res, 0, 1)}')
expect_type="l"
if [ "$expect_type" != "$file_type" ]; then
  ln -s "${current_dir}/rofi/" ~/.config/
fi
